package com.preskool.service.impl;

import com.preskool.dto.TimeTableVo;
import com.preskool.mapper.TimeTableMapper;
import com.preskool.po.TimeTable;
import com.preskool.po.TimeTableExample;
import com.preskool.service.TimeTableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: lihao
 * @email: 2470849570@qq.com
 * @date: 20212021/1/9
 * @describe: 针对无中生有时间表做出的增删查改
 * @version: v1.0
 */
@Service
@Slf4j
public class TimeTableServiceImpl implements TimeTableService {

    @Autowired
    private TimeTableMapper timeTableMapper;

    /**
     * @param currentPage 当前页
     * @param pageSize    最大页码
     * @return
     */
    @Override
    public List<TimeTableVo> findAll(Integer currentPage, Integer pageSize) {


        //创建查询模板
        TimeTableExample example = new TimeTableExample();
        //设置当前页
        currentPage = (currentPage - 1) * pageSize;
        //设置当前页
        example.setOffset(currentPage);
        //设置每页显示最大条数
        example.setOffset(pageSize);
        System.out.println(currentPage+"*******"+pageSize);

        List<TimeTableVo> timelist = timeTableMapper.queryAll(example);
        for (TimeTableVo tt : timelist) {
            log.info(tt.toString());
        }
        return timelist;
    }

    /**
     * 根据id查询TimeVo
     *
     * @param ttid
     * @return
     */
    @Override
    public TimeTableVo findOne(Integer ttid) {
        TimeTableVo timeTableVo = timeTableMapper.queryOne(ttid);
        return timeTableVo;
    }

    /**
     * 根据ttid 删除原来时间表状态
     *
     * @param ttid
     * @return
     */
    public int deleteOne(Integer ttid) {
        //查询ttid对应的数据是否存在
        TimeTable table = timeTableMapper.queryTableByTtid(ttid);
        if (table != null) {
            int i = timeTableMapper.deleteByPrimaryKey(ttid);
            return i;
        } else {
            return -1;
        }
    }

    /**
     * 添加TimeTableVo对象
     *
     * @param timeTable
     * @return
     */
    @Override
    public int addTimeTable(TimeTable timeTable) {
        //选择性插入数据
        int i = timeTableMapper.insertSelective(timeTable);
        return i;
    }

    /**
     * 更新无中生有的TimeTable表
     *
     * @param timeTable
     * @return
     */
    @Override
    public int updateTimeTable(TimeTable timeTable) {
        //更新前先查询
        TimeTable ff = timeTableMapper.queryTableByTtid(timeTable.getTtid());
        if (ff != null) {
            //选择性更新
            int i = timeTableMapper.updateByPrimaryKeySelective(timeTable);
            return i;
        } else {
            return -1;
        }
    }
}
